﻿const cloud = require('wx-server-sdk');

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();

const normalizeString = value =>
  typeof value === "string" ? value.trim() : "";

const main = async event => {
  const activityID = normalizeString(event && event.activityID);
  if (!activityID) {
    return {
      needCollectInfo: false,
      fields: [],
      error: "缺少活动编号"
    };
  }

  try {
    const res = await db.collection("Activity_Registration_Information_Fields")
      .where({ ActivityID: activityID })
      .orderBy("_id", "asc")
      .get();

    const fields = (res.data || []).map(item => ({
      fieldID: item._id,
      fieldName: item.FieldName || "",
      isOptional: !!item.IsOptional
    }));

    return {
      needCollectInfo: fields.length > 0,
      fields
    };
  } catch (error) {
    return {
      needCollectInfo: false,
      fields: [],
      error: error.message || "查询失败"
    };
  }
};

exports.main = main;

